package com.chongqing.shiro.service.impl;

import com.chongqing.shiro.mapper.PermissionMapper;
import com.chongqing.shiro.mapper.RoleMapper;
import com.chongqing.shiro.mapper.RolePermissionMapper;
import com.chongqing.shiro.mapper.UserMapper;
import com.chongqing.shiro.model.entity.Permission;
import com.chongqing.shiro.model.entity.Role;
import com.chongqing.shiro.model.entity.RolePermission;
import com.chongqing.shiro.model.entity.User;
import com.chongqing.shiro.model.vo.ResponseVo;
import com.chongqing.shiro.model.vo.ResultUtil;
import com.chongqing.shiro.service.RoleService;
import com.chongqing.shiro.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public Set<String> findRoleByUserId(String userId) {
        return roleMapper.findRoleByUserId(userId);
    }

    @Override
    public List<Role> selectRoles(Role role) {
        return roleMapper.selectRoles(role);
    }

    @Override
    public int insert(Role role) {
        role.setRoleId(UUIDUtil.getHashUUID());
        role.setCreateTime(new Date());
        role.setStatus(1);
        return roleMapper.insert(role);
    }

    @Override
    public List<User> findRoleId(String roleId) {
        return userMapper.findRoleId(roleId);
    }

    @Override
    public int updateStatusBatch(List<String> roleIdsList, Integer status) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("roleIds", roleIdsList);
        params.put("status", status);
        return roleMapper.updateStatusBatch(params);
    }

    @Override
    public List<User> findRoleIds(List<String> roleIdsList) {
        return userMapper.findByRoleIds(roleIdsList);
    }

    @Override
    public Role findById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByRoleId(Role role) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("name", role.getName());
        params.put("description", role.getDescription());
        params.put("role_id", role.getRoleId());
        return roleMapper.updateByRoleId(params);
    }

    @Override
    public List<Permission> findPermissionByRoleId(String roleId) {
        return permissionMapper.findByRoleId(roleId);
    }

    @Override
    public ResponseVo addAssifnPermission(String roleId, List<String> permissionIdList) {
        try {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermissionMapper.delete(rolePermission);
            for (String permissionId : permissionIdList) {
                rolePermission.setId(null);
                rolePermission.setPermissionId(permissionId);
                rolePermissionMapper.insert(rolePermission);
            }
            return ResultUtil.success("分配权限成功");
        } catch (Exception e) {
            return ResultUtil.error("分配权限权限失败");
        }
    }

    @Override
    public List<User> findByRoleId(String roleId) {
        return userMapper.findRoleId(roleId);
    }
}
